from pyparsing import *

def removeComments(string):
    chars = printables + " \t\n" #also needs ASCII 0 to 6
    cchars = chars.replace("[","").replace("]","")
    qchars = chars.replace("'", "")
    quote = "'" + Word(qchars) + "'" #no support for '' escaping
    comment = Forward()
    comment << Suppress('[') + ZeroOrMore(comment | Word(cchars)) + Suppress(']')
    text = ZeroOrMore(quote | Word(cchars) | Suppress(comment))
    return "".join(text.parseString(string).asList())


def testRemoveComments():
    tests = """\
        The [quick] [brown] fox jumped over the [lazy] dog
        The [[quick] brown] fox jumped over the [lazy] dog
        The [quick [brown]] fox jumped over the [crazy [lazy [sleeping]]] dog
        noncommen[comment in the middle of a]t t[o]oken
        ['you shouldn't see this'] but '[you should see this]'"""
    tests = tests.splitlines()
    for t in tests:
        print "before: \"" + t.strip() + "\""
        print "after:  \"" + removeComments(t) + "\""

def runTests():
    testRemoveComments()

if __name__ == "__main__":
    runTests()




